//
//  WMERegisterViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/27.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMERegisterViewController.h"
#import <Masonry.h>
#import <SMS_SDK/SMSSDK.h>
@interface WMERegisterViewController ()
@property (nonatomic, retain)UITextField *phoneNumber;
@property (nonatomic, retain)UITextField *validateCode;
@end

@implementation WMERegisterViewController

- (void)dealloc
{
    [_phoneNumber release];
    [_validateCode release];
    [super dealloc];
}
#pragma mark -视图已经加载
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   
    
    ////////////////自治导航栏//////////////////
    UIView *ttview = [[UIView alloc] init];
//    ttview.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    ttview.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.view addSubview:ttview];
    //view布局
    [ttview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 64));
        
        make.centerX.mas_equalTo(self.view.center);
        make.top.equalTo(self.view).with.offset(0);
        make.left.equalTo (self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    [ttview release];
    
    //线
    UILabel *lll = [[UILabel alloc] init];
    //    __block WMEFindViewController *find = self;
    lll.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lll];
    [lll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 0.3));
        
        make.centerX.mas_equalTo(self.view.center);
        make.top.equalTo(self.view).with.offset(64);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    [lll release];
    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.backgroundColor = [UIColor greenColor];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    [ttview addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 46));
        make.left.equalTo(ttview.mas_left).with.offset(10);
        make.top.equalTo(ttview.mas_top).with.offset(20);
    }];
    
    //确定按钮
    UIButton *queding = [UIButton buttonWithType:UIButtonTypeSystem];
//    queding.backgroundColor = [UIColor cyanColor];
    [queding setTitle:@"确定" forState:UIControlStateNormal];
    [queding addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [ttview addSubview:queding];
    [queding mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 46));
        make.right.equalTo(ttview.mas_right).with.offset(- 10);
        make.top.equalTo(ttview.mas_top).with.offset(20);
    }];

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc ]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(btnAction)];
//    
    self.view.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    //使用手机注册label
    UILabel * shoujizhuce = [[UILabel alloc] init];
    shoujizhuce.text = @"使用手机号码注册";
    shoujizhuce.font = [UIFont systemFontOfSize:13];
    shoujizhuce.textColor = [UIColor grayColor];
    [self.view addSubview:shoujizhuce];
    [shoujizhuce release];
    
    //注册View
    UIView *registerView = [[UIView alloc] init];
    registerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:registerView];
    [registerView release];
    
    //获取验证码Button
    UIButton *acquireButton = [UIButton buttonWithType:UIButtonTypeCustom];
    acquireButton.backgroundColor = [UIColor colorWithRed:75/255.0 green:127/255.0 blue:245/255.0 alpha:1];
    acquireButton.layer.cornerRadius = 3;
    acquireButton.clipsToBounds = YES;
    [acquireButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    acquireButton.backgroundColor = [UIColor colorWithRed:20/255.0 green:180/255.0 blue:243/255.0 alpha:1];
    acquireButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [acquireButton addTarget:self action:@selector(huoquyanzhengma:) forControlEvents:UIControlEventTouchUpInside];
    
    [registerView addSubview:acquireButton];
    
    //号码前缀
    UILabel *prefix = [[UILabel alloc] init];
    prefix.font = [UIFont systemFontOfSize:15];
    prefix.text = @"+86";
    [registerView addSubview:prefix];
    [prefix release];
    
    //手机号码
    self.phoneNumber = [[UITextField alloc] init];
    _phoneNumber.font = [UIFont systemFontOfSize:15];
    _phoneNumber.placeholder = @"输入你的手机号";
    [registerView addSubview:_phoneNumber];
    [_phoneNumber release];
    
    //验证码
    self.validateCode = [[UITextField alloc] init];
    _validateCode.font = [UIFont systemFontOfSize:15];
    _validateCode.placeholder = @"输入验证码";
    [registerView addSubview:_validateCode];
    [_validateCode release];
    __block __typeof(&*self)rvc = self;
    //设置提示label的位置
    [shoujizhuce mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.left.equalTo(rvc.view).with.offset(10);
        make.top.equalTo(rvc.view).with.offset(100);
        make.right.equalTo(rvc.view).with.offset(-10);
    }];
    //设置view的位置
    [registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.top.equalTo(shoujizhuce.mas_bottom).with.offset(10);
        make.right.left.equalTo(rvc.view).with.offset(0);
    }];
    
    //设置按钮的位置
    [acquireButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 34));
        make.top.equalTo(registerView.mas_top).with.offset(8);
        make.right.equalTo(registerView.mas_right).with.offset(-10);
    }];
    
    //前缀的位置
    [prefix mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registerView.mas_top).with.offset(10);
        make.left.equalTo(registerView.mas_left).with.offset(10);
        make.width.height.equalTo(@30);
    }];
    
    //号码的位置
    [_phoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(50, 50));
        make.left.equalTo(prefix.mas_right).with.offset(15);
        make.right.equalTo(acquireButton.mas_left).with.offset(- 10);
        make.top.equalTo(registerView.mas_top).with.offset(0);
        make.height.equalTo(@50);
    }];
    
    //验证码的位置
    [_validateCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneNumber.mas_bottom).with.offset(0);
        make.right.equalTo(_phoneNumber.mas_right).with.offset(0);
        make.width.equalTo(_phoneNumber.mas_width).with.offset(0);
        make.height.equalTo(@50);
        
    }];
    
    //视图的美化
    UILabel *line0 = [[UILabel alloc] init];
    UILabel *line1 = [[UILabel alloc] init];
    UILabel *line2 = [[UILabel alloc] init];
    
    //线的背景 颜色是 亮灰
    line0.backgroundColor = [UIColor lightGrayColor];
    line1.backgroundColor = [UIColor lightGrayColor];
    line2.backgroundColor = [UIColor lightGrayColor];
    
    //添加视图
    [registerView addSubview:line0];
    [registerView addSubview:line1];
    [registerView addSubview:line2];
    
    //设置在 View 上部的线
    [line0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registerView.mas_top).with.offset(0);
        make.right.equalTo(registerView.mas_right).with.offset(0);
        make.left.equalTo(registerView.mas_left).with.offset(0);
        make.height.equalTo(@0.3);
    }];
    
    //设置在 view 中部的线
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneNumber.mas_bottom).with.offset(0);
        make.right.equalTo(registerView.mas_right).with.offset(0);
        make.left.equalTo(registerView.mas_left).with.offset(10);
        make.height.equalTo(@0.3);
    }];
    
    //设置在 view 底部的线
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.3);
        make.left.right.equalTo(registerView).with.offset(0);
        make.bottom.equalTo(registerView.mas_bottom).with.offset(0);
    }];
    [line0 release];
    [line1 release];
    [line2 release];
    
}
#pragma mark -验证 验证码 是否 正确
- (void)btnAction
{
    if (_phoneNumber.text.length != 0 && _validateCode != 0) {
        /**
         * @from               v1.1.1
         * @brief              提交验证码(Commit the verification code)
         *
         * @param code         验证码(Verification code)
         * @param phoneNumber  电话号码(The phone number)
         * @param zone         区域号，不要加"+"号(Area code)
         * @param result       请求结果回调(Results of the request)
         */
        [SMSSDK commitVerificationCode:_validateCode.text phoneNumber:_phoneNumber.text zone:@"86" result:^(NSError *error) {
            
            if (!error) {
                NSLog(@"验证成功");
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setBool:YES forKey:@"success"];
                [user setObject:_phoneNumber.text forKey:@"userName"];
            }
            else
            {
                NSLog(@"错误信息:%@",error);
            }
        }];
    }
    else{
        [self tishiView:@"手机号码或验证码不能为空"];
    }
}


#pragma mark -获取验证码的方法
- (void)huoquyanzhengma:(UIButton *)btn
{
    if (_phoneNumber.text.length != 0) {
        /**
         *  @from                    v1.1.1
         *  @brief                   获取验证码(Get verification code)
         *
         *  @param method            获取验证码的方法(The method of getting verificationCode)
         *  @param phoneNumber       电话号码(The phone number)
         *  @param zone              区域号，不要加"+"号(Area code)
         *  @param customIdentifier  自定义短信模板标识 该标识需从官网http://www.mob.com上申请，审核通过后获得。(Custom model of SMS.  The identifier can get it  from http://www.mob.com  when the application had approved)
         *  @param result            请求结果回调(Results of the request)
         */
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneNumber.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (!error) {
                NSLog(@"获取验证码成功");
            } else {
                NSLog(@"错误信息：%@",error);
            };
        }];
    }else{
        [self tishiView:@"手机号不能为空"];
    }

}




#pragma mark -弹出提示窗的按钮
- (void)tishiView:(NSString *)str
{
    //弹出的弹窗View
    UIView *tsView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , WIDTH, HEIGHT)];
    tsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    tsView.tag = 9999;
    tsView.userInteractionEnabled = YES;
    [self.view addSubview:tsView];
    [tsView release];
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chuxian:)];
    [tsView addGestureRecognizer:tap];
    //弹出的View中 白色的那个view 用来放置 其他视图的背景
    UIView *beijing = [[UIView alloc] init];
    beijing.backgroundColor = [UIColor whiteColor];
    [tsView addSubview:beijing];
    [beijing release];
    //添加显示 提示语句的Label
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:15];
    label.text = str;
//    label.numberOfLines = 0;
//    label.backgroundColor = [UIColor yellowColor];
    [beijing addSubview:label];
    [label release];
    //装饰用的button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitle:@"知道了" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chuxian2:) forControlEvents:UIControlEventTouchUpInside];
//    button.backgroundColor = [UIColor redColor];
    [beijing addSubview:button];
    
    __block __typeof(&*self)ws = self;
    //背景的布局
    [beijing mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.left.equalTo(ws.view.mas_left).with.offset(10);
        make.right.equalTo(ws.view.mas_right).with.offset(-10);
        make.centerY.equalTo(ws.view.mas_centerY);
    }];
    //显示提示语句的label 的 布局
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 60));
        make.left.equalTo(beijing.mas_left).with.offset(20);
        make.top.equalTo(beijing.mas_top).with.offset(20);
        make.right.equalTo(beijing.mas_right).with.offset(-120);
        make.centerY.mas_equalTo(ws.view.mas_centerY);
    }];
    //按钮的布局 为位于屏幕的 右下角
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 30));
        make.right.equalTo(beijing.mas_right).with.offset(-20);
        make.bottom.equalTo(beijing.mas_bottom).with.offset(-20);
    }];

}

#pragma mark-返回按钮 触发事件
- (void)fanhui
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"返回啦");
    }];
}
#pragma mark -点击灰色View触发的事件
- (void)chuxian:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 9999) {
        [tap.view removeFromSuperview];
    }
}
#pragma mark -点击弹出视图上的按钮触发的事件
- (void)chuxian2:(UIButton *)btn
{
    [btn.superview.superview removeFromSuperview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
